﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using EntityManage;
using BusinessFacade;

public partial class Modules_Personal_PerfModify : System.Web.UI.Page
{
    IPerf_Personal_ApprovalHandler approvalHandler = new Perf_Personal_ApprovalManage();
    ISys_UserHandler userHandler = new Sys_UserManage();
    ISys_OrganizationHandler orgHandler = new Sys_OrganizationManage();
    ISys_OrganizationHandlerExt orgExtHandler = new Sys_OrganizationManageExt();
    ISys_PostHandler postHandler = new Sys_PostManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            ViewState["OrderBy"] = "ID";
            ViewState["Accending"] = "desc";

            dataBind(" 1=1 ", null);
          
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("303060"))
            {
                
            }
            else
            {
                return false;
            }
            return true;
        }
        else
        {
            return false;
        }
    }

    #region DataBind
    
    /// <summary>
    /// 初始化岗位信息表
    /// </summary>
    private void dataBind(string whereClause, SqlParameter[] parms)
    {
        try
        {
            int counter = 0;
            DataTable dt = new DataTable();

            // 自己呈批的绩效得分记录
            int userId = int.Parse(Session["CurrentUserID"].ToString());

            whereClause += " and User_ID =" + userId;

            dt = approvalHandler.GetPerf_Personal_ApprovalByPageList(whereClause, parms, ViewState["OrderBy"].ToString() + " " + ViewState["Accending"].ToString(), AspNetPager.StartRecordIndex - 1, AspNetPager.PageSize);

            counter = approvalHandler.GetCounter(whereClause, parms);

            this.AspNetPager.RecordCount = counter;
            gvPerfApproveList.DataSource = dt;
            gvPerfApproveList.DataBind();
        }
        catch
        {
            Jscript.AlertInAjax("連接錯誤!", UpdatePanel1);
            return;
        }
    }

    /// <summary>
    /// 条件参数处理
    /// </summary>
    /// <param name="whereClause"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string whereClause, ref SqlParameter[] parms)
    {
        List<SqlParameter> lstParm = new List<SqlParameter>();

        if (ViewState["SearchPerfDate"] != null)
        {
            whereClause += " and Perf_Date = @Perf_Date";
            lstParm.Add(new SqlParameter("@Perf_Date", ViewState["SearchPerfDate"].ToString()));
        }

        parms = lstParm.ToArray();
    }

    #endregion

    #region gvPerfApproveList

    protected void gvPerfApproveList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
    }

    protected void gvPerfApproveList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex > -1)
            {
                Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(int.Parse(e.Row.Cells[3].Text));
                if (userQuery != null)
                {
                    e.Row.Cells[3].Text = userQuery.Name;

                    Sys_OrganizationQuery orgQuery = orgHandler.GetSys_OrganizationQuery(userQuery.Org_ID);
                    if(orgQuery != null)
                        e.Row.Cells[1].Text = orgQuery.Name;
                    else
                        e.Row.Cells[1].Text = "";

                    Sys_PostQuery postQuery = postHandler.GetSys_PostQuery(userQuery.Post_ID);
                    if (postQuery != null)
                        e.Row.Cells[2].Text = postQuery.Name;
                    else
                        e.Row.Cells[2].Text = "";

                }

                // 状态
                Enums.PersonalApprovalStatus status = (Enums.PersonalApprovalStatus)int.Parse(e.Row.Cells[7].Text);
                e.Row.Cells[7].Text = Enums.GetPersonalApprovalStatus(status);
                ImageButton ibtnEdit = (ImageButton)e.Row.FindControl("ibtnEdit");
                ibtnEdit.Visible = (status == Enums.PersonalApprovalStatus.Notice);
            }
        }
    }

    protected void gvPerfApproveList_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        RefreshDataGridView();
    }

    #endregion

    #region Button
    
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtPerfDate.Text != null && !"".Equals(txtPerfDate.Text))
            ViewState["SearchPerfDate"] = txtPerfDate.Text;
        else
            ViewState["SearchPerfDate"] = null;

        RefreshDataGridView();
    }

    protected void AspNetPager_PageChanged(object src, EventArgs e)
    {
        RefreshDataGridView();
    }

    #endregion

    private void RefreshDataGridView()
    {
        SqlParameter[] parms = null;
        string clause = " 1=1 ";
        ProcessParms(ref clause, ref parms);
        dataBind(clause, parms);
    }

}
